﻿using System.ServiceModel.DomainServices.EntityFramework;
using Falafel.Services.Web.Models;
using Falafel.Services.Web;

namespace Falafel.Model.Services
{
    public class FalafelBaseService : LinqToEntitiesDomainService<FalafelEntities>
    {
        public FalafelBaseService(User currentUser)
        {
            CurrentUser = currentUser;
        }

        public User CurrentUser { get; set; }

        /// <summary>
        /// Populates the CreatedBy/ModifiedBy fields.
        /// </summary>
        /// <param name="item">An item that supports IAuditable</param>
        /// <param name="isInsert">True if this is an insert (set CreatedBy)</param>
        protected void SetAuditingFields(object item, bool isInsert)
        {
            if (!(item is IAuditable))
            {
                return;
            }
            IAuditable auditable = (IAuditable)item;
            if (isInsert)
            {
                auditable.CreatedBy = CurrentUser.ID;
            }
            auditable.ModifiedBy = CurrentUser.ID;
        }

    }
}